﻿using FlexFlow.Base.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FlexFlow.DataProvider.MongoDB;
using System.Reflection;
using System.Web;

namespace FlexFlow.Runtime
{
    /// <summary>
    /// 默认组件设置类
    /// </summary>
    public static class DefaultComponentSettings
    {
        #region======数据提供者======

        /// <summary>
        /// 获取或设置引擎的默认用户数据提供器
        /// </summary>
        public static IUserProvider UserProvider { get; set; }
        /// <summary>
        /// 获取或设置引擎的默认组织机构数据提供其
        /// </summary>
        public static IOrganizeProvider OrganizeProvider { get; set; }
        /// <summary>
        /// 获取或设置默认用户关系数据提供器
        /// </summary>
        public static IUserRelationProvider UserRelationProvider { get; set; }
        /// <summary>
        /// 获取或设置默认文件描述提供器
        /// </summary>
        public static IFileDescriptionProvider FileDescriptionProvider { get; set; }
        /// <summary>
        /// 获取或设置默认的工作流表单模板提供器
        /// </summary>
        public static IFormTemplateProvider FormTemplateProvider { get; set; }
        /// <summary>
        /// 获取或设置默认工作流表单提供器
        /// </summary>
        public static IWorkFlowFormProvider WorkFlowFormProvider { get; set; }
        /// <summary>
        /// 获取或设置默认的物理文件提供器
        /// </summary>
        public static IPhysicalFileProvider PhysicalFileManager { get; set; }
        /// <summary>
        /// 获取或设置默认的通知文件夹提供器
        /// </summary>
        public static INotificationCatalogProvider NotificationCatalogProvider { get; set; }
        /// <summary>
        /// 获取或设置默认的通知提供器
        /// </summary>
        public static INotificationProvider NotificationProvider { get; set; }
        /// <summary>
        /// 获取或设置默认的流程提供器
        /// </summary>
        public static IProcessProvider ProcessProvider { get; set; }
        /// <summary>
        /// 获取或设置默认的流程实例提供器
        /// </summary>
        public static IProcessInstanceProvider ProcessInstanceProvider { get; set; }
        /// <summary>
        /// 获取或设置默认的活动实例提供器
        /// </summary>
        public static IActivityInstanceProvider ActivityInstanceProvider { get; set; }
        /// <summary>
        /// 获取或设置默认的工作流任务提供器
        /// </summary>
        public static IWorkFlowTaskProvider WorkFlowTaskProvider { get; set; }
        /// <summary>
        /// 获取或设置默认的权限定义提供器
        /// </summary>
        public static IPermissionDefineProvider PermissionDefineProvider { get; set; }
        /// <summary>
        /// 获取或设置默认的用户权限提供器
        /// </summary>
        public static IUserPermissionProvider UserPermissionProvider { get; set; }
        /// <summary>
        /// 获取或设置默认的工作流信息提供器
        /// </summary>
        public static IWorkFlowInformationProvider WorkFlowInformationProvider { get; set; }
        /// <summary>
        /// 获取或设置默认的流程步骤提供器
        /// </summary>
        public static IFlowSetpProvider FlowSetpProvider { get; set; }
        /// <summary>
        /// 获取或设置引擎的默认在线用户管理池
        /// </summary>
        public static IOnlineUserPool OnlineUserPool { get; set; }
        /// <summary>
        /// 获取或设置引擎的默认应用程序池
        /// </summary>
        public static IApplicationPool ApplicationPool { get; set; }

        #endregion
        #region========管理器========
        /// <summary>
        /// 获取或设置引擎的默认用户管理器
        /// </summary>
        public static UserManager UserManager { get; set; }
        /// <summary>
        /// 获取或设置引擎的默认用户关系管理器
        /// </summary>
        public static UserRelationManager UserRelationManager { get; set; }
        /// <summary>
        /// 获取或设置引擎的默认用户数据管理器
        /// </summary>
        public static UserDataManager UserDataManager { get; set; }
        /// <summary>
        /// 获取或设置引擎的默认组织机构管理器
        /// </summary>
        public static OrganizeManager OrganizeManager { get; set; }
        /// <summary>
        /// 获取或设置引擎的默认表单模板管理器
        /// </summary>
        public static FormTemplateManager FormTemplateManager { get; set; }
        /// <summary>
        /// 获取或设置引擎的默认通知管理器
        /// </summary>
        public static NotificationManager NotificationManager { get; set; }
        /// <summary>
        /// 获取或设置引擎的默认流程定义管理器
        /// </summary>
        public static ProcessDefineManager ProcessDefineManager { get; set; }
        /// <summary>
        /// 获取或设置引擎的默认工作流表单管理器
        /// </summary>
        public static WorkFlowFormManager WorkFlowFormManager { get; set; }
        #endregion
        /// <summary>
        /// 初始化引擎运行时的默认设定
        /// </summary>
        static DefaultComponentSettings()
        {
            OnlineUserPool = new InProcOnlineUserPool();
            ApplicationPool = new InProcApplicationPool();
            UserProvider = new MongoDbUserProvider();
            FormTemplateProvider = new MongoDbFormTemplateProvider();
            OrganizeProvider = new MongoDbOrganizeProvider();
            UserRelationProvider = new MongoDbUserRelationProvider();
            FileDescriptionProvider = new MongoDbFileDescriptionProvider();
            WorkFlowFormProvider = new MongoDbWorkFlowFormProvider();
            PhysicalFileManager = new LocalDiskFileProvider();
            NotificationCatalogProvider = new MongoDbNotificationCatalogProvider();
            NotificationProvider = new MongoDbNotificationProvider();
            WorkFlowTaskProvider = new MongoDbWorkFlowTaskProvider();
            ProcessProvider = new MongoDbProcessProvider();
            PermissionDefineProvider = new MongoDbPermissionDefineProvider();
            UserPermissionProvider = new MongoDbUserPermissionProvider();
            WorkFlowInformationProvider = new MongoDbWorkFlowInformationProvider();
            FlowSetpProvider = new MongoDbFlowSetpProvider();

            ProcessInstanceProvider = new MongoDbProcessInstanceProvider();
            ActivityInstanceProvider = new MongoDbActivityInstanceProvider();

            BuildService.FormTemplateCompiler.Default.Start();
            BuildService.ProcessInstaller.Default.Start();
            RegisterApplication();

            UserManager umanager = new UserManager();
            /********************************************************************/
        }

        static void RegisterApplication()
        {
            string assemblyPath;
            if (HttpContext.Current != null)
            {
                assemblyPath = HttpContext.Current.Server.MapPath("~/bin/");
            }
            else
            {
                assemblyPath = Environment.CurrentDirectory;
            }
            Assembly asm = Assembly.LoadFrom(assemblyPath + "/FlexFlow.Runtime.Applications.dll");
            foreach (var type in asm.GetTypes())
            {
                if (typeof(IApplicationBucket).IsAssignableFrom(type))
                {
                    IApplicationBucket bucket = (IApplicationBucket)(Activator.CreateInstance(type));
                    bucket.RegisterApplication(ApplicationPool);
                }
            }
            //ApplicationPool.RegisterApplication("start", new StartActivityApplication(), actDescription: "开始活动", cssClass: "round");
            //ApplicationPool.RegisterApplication("end", new EndActivityApplication(), actDescription: "终止活动", cssClass: "round");
            //ApplicationPool.RegisterApplication("task", new TaskApplication(), actDescription: "任务活动");
        }

    }
}
